package com.suikajy.damaiproject.ui.main_class;


import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.suikajy.damaiproject.App;
import com.suikajy.damaiproject.R;
import com.suikajy.damaiproject.bean.ClassBean;
import com.suikajy.damaiproject.config.UrlConfig;
import com.suikajy.damaiproject.ui.MainActivity;
import com.suikajy.damaiproject.util.HttpUtils;
import com.suikajy.damaiproject.util.PullDownRefreshLayout;

import java.util.TreeSet;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClassContentFragment extends Fragment implements PullDownRefreshLayout.OnRefreshListener {

    @Bind(R.id.rv)
    RecyclerView mRv;
    private String mPath;
    private int pageIndex = 1;
    private ClassAdapter mAdapter;
    private boolean canRefresh = false;
    private LinearLayoutManager mLayoutManager;
    private int mFirstVisibleItem;
    private PullDownRefreshLayout mRefreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_class_content, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle arguments = getArguments();
        int mc = arguments.getInt("mc");
        mPath = UrlConfig.getClassUrlByMc(mc, pageIndex);
        getData();
    }

    /**
     * 开启io线程加载网络数据
     */
    private void getData() {
        HttpUtils.getDataBean(mPath, ClassBean.class, new HttpUtils.OnGetBean<ClassBean>() {
            @Override
            public void onGetBean(ClassBean bean) {
                setRecyclerView(bean);
            }
        });
    }

    private void setRecyclerView(ClassBean classBean) {
        mLayoutManager = new LinearLayoutManager(MainActivity.getInstance());
        mRv.setLayoutManager(mLayoutManager);
        mRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                int offset = MainActivity.getInstance().getResources().getDimensionPixelOffset(R.dimen.default_margin);
                outRect.set(0, offset, 0, offset);
            }
        });
        mAdapter = new ClassAdapter();
        mRv.setAdapter(mAdapter);
        mAdapter.setData(classBean);
        canRefresh = true;
        mRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                //Log.e("TAG", "new State is " + newState);
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING && mFirstVisibleItem == 0) {
                    //Log.e("TAG", "此处发送广播");
                    Intent intent=new Intent();
                    intent.setAction(PullDownRefreshLayout.RECEIVE_ACTION);
                    App.getInstance().sendBroadcast(intent);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                mFirstVisibleItem = mLayoutManager.findFirstCompletelyVisibleItemPosition();
                //Log.e("TAG", "onScrolled ===> dy is " + dy);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mRefreshLayout = ((ClassFragment)getParentFragment()).getRefreshLayout();
        mRefreshLayout.setRefreshListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        
    }
}
